
/* GCSx
** WORLDPROP.CPP
**
** World properties dialog
*/

/*****************************************************************************
** Copyright (C) 2003-2006 Janson
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
*****************************************************************************/

#include "all.h"

// Properties dialog

WorldPropertiesDialog* WorldPropertiesDialog::dialog = NULL;

WorldPropertiesDialog* WorldPropertiesDialog::create() { start_func
    if (!dialog) {
        dialog = new WorldPropertiesDialog();
    }
    return dialog;
}

void WorldPropertiesDialog::destroy() { start_func
    if (dialog) {
        delete dialog;
        dialog = NULL;
    }
}

WorldPropertiesDialog::WorldPropertiesDialog() : Dialog("World Properties") { start_func
    initialized = 0;
    tree = NULL;
}

WorldPropertiesDialog::~WorldPropertiesDialog() { start_func
    if (tree) {
        WidgetScroll* ws = dynamic_cast<WidgetScroll*>(tree->getParent());
        if (ws) ws->newClient();
        delete tree;
    }
}

Dialog::ButtonAction WorldPropertiesDialog::verifyEntry(int buttonId, Dialog::ButtonAction buttonType) { start_func
    if ((buttonType != BUTTON_APPLY) && (buttonType != BUTTON_OK)) return BUTTON_DEFAULT;

    const string& title = dynamic_cast<WTextBox*>(findWidget(ID_TITLE))->state();

    if (title.empty()) {
        guiErrorBox("Please enter a title for this world.", errorTitleMissingName);
        return BUTTON_NOTHING;
    }

    return Dialog::verifyEntry(buttonId, buttonType);
}

int WorldPropertiesDialog::run(string* title, int* startScene, const WorldEdit* whichWorld) { start_func
    if (!initialized) {
        Widget* w = NULL;

        w = new WStatic(ID_LABEL, "\tTitle:");
        w->addTo(this);

        w = new WTextBox(ID_TITLE, title, 0);
        w->addTo(this);

        w = new WStatic(ID_LABEL, "\tStarting scene:");
        w->addTo(this);

        tree = new TreeView(blankString, NULL, 0, NULL, 1);
        tree->addTo(this, -1, 5);

        w = new WButton(ID_OK, messageBoxOK, BUTTON_OK);
        w->addTo(this);

        w = new WButton(ID_CANCEL, messageBoxCancel, BUTTON_CANCEL);
        w->addTo(this);

        makePretty(2, 0);

        initialized = 1;
    }
    else {
        findWidget(ID_TITLE)->changeStorage(title);
    }

    TreeView* item;
    tree->removeAll();
    tree->insert(new TreeView("(none)", this, 0, NULL), *startScene == 0);
    
    for (World::SceneIndex::const_iterator pos = whichWorld->beginScene(); pos != whichWorld->endScene(); ++pos) {
        const Scene* toAdd = (*pos).second;
        tree->insert(item = new TreeView(toAdd->getName(), this, toAdd->getId(), NULL), *startScene == toAdd->getId());
        item->setIcon(6);
    }

    // Hit OK?
    if (runModal() == ID_OK) {
        // Scene
        item = tree->findSelected();
        if (item) *startScene = item->getCode();
        else *startScene = 0;
        
        return 1;
    }
    
    return 0;
}

